<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="yongjian.drp.model.User" %>
<%@ taglib uri="/struts-tags" prefix="s" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

User recorder = (User)session.getAttribute("user");
String name = recorder.getUsername();
int id = recorder.getId();
String adjustTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//Date adjustTime = new Date();
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'user_add.jsp' starting page</title>
    
    <link rel="stylesheet" type="text/css" href="js/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="js/themes/icon.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="js/locale/easyui-lang-zh_CN.js"></script>
    
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript">
		var flag;
		$(function(){				
			 $('#grid').datagrid({   
			  	  idField:'id',
			  	  title:'流向单维护表',
			      url:'json/flowCard_list.action?vouSts=N', 
			      striped:true,
			      loadMsg:'正在加载中，请等待....',
			      pagination:true,
			      fitColumns:true,
			      rownumbers:true,
			      pageSize:7,
			      pageList:[7,14,21,28,35],
			      
			      frozenColumns:[
									[{
										field:'cb',
										checkbox:true									
									 }]				               
					               ],
			      columns:[[   
			          {field:'id',title:'流向单号',width:100},   
			          {field:'clientId',title:'供方分销商代码',width:100},   
			          {field:'clientName',title:'供方分销商名称',width:100},   
			          {field:'recorder',title:'录入人',width:100},  
			          {field:'recordTime',title:'录入时间',width:100,formatter:function(value){
			        	  return value.replace("T","  ");
			          }},
			      ]] ,
			      toolbar:[{
						text:'新增流向单' ,
						iconCls:'icon-add' , 
						handler:function(){
							flag = 'add';
							$('#dialog').dialog({
									title:'新增流向单' 
							});
							$('#myform').get(0).reset();
							$('#dialog').dialog('open');
							document.getElementById("recorderName").value="<%=name%>";
							document.getElementById("recorderId").value="<%=id%>";
							
							$('#clientName').combobox({
								 url:'json/client_getTrader.action?isClient=true',
								 editable:false,
								 valueField:'id',   
								 textField:'text',
								 value:'',
								 onSelect:function(record){
										document.getElementById("clientId").value=record.id;
										document.getElementById("clientId").readOnly=true;
								}
							 });
						
						}							
					},{
						text:'编辑流向单',
						iconCls:'icon-edit',
						handler:function(){
							flag = 'edit';							
							var arr = $('#grid').datagrid('getSelections');
							if(arr.length == 1){
								$('#myform').get(0).reset();
								$('#dialog').dialog('open');								
								document.getElementById("id").value=arr[0].id;
								document.getElementById("recorderName").value="<%= name%>";
								document.getElementById("clientId").value=arr[0].clientId;
								//异步加载
								$('#clientName').combobox({
									 url:'json/client_getTrader.action?isClient=true',
									 editable:false,
									 valueField:'id',   
									 textField:'text',
									 value:arr[0].clientName,
									 onSelect:function(record){
											document.getElementById("clientId").value=record.id;
											document.getElementById("clientId").readOnly=true;
									}
								 });
																							
							} else {
								$.messager.alert('提示','请选择一条流向单编辑','warning'); 
							}
						}
					},{
						text:'送审',
						iconCls:'icon-ok',
						handler:function(){
							var arr = $('#grid').datagrid('getSelections');
							if(arr.length == 1){
								$.ajax({
									url:'json/flowCardDetail_isExist.action',
									cache:false,
									data:{flowCardId:arr[0].id},
									dataType:'json',
									success:function(result){
										if(result.status == "notExist"){
											$.messager.alert('提示',result.msg);
										} 
										if(result.status == "exist"){
											$.messager.confirm('确认','可以送审吗？',function(r){
												if(r){
													$.post('json/flowCard_modify',
															{"flowCard.vouSts":'S',"flowCard.id":arr[0].id},
															function(){
											        			   $('#grid').datagrid('reload');
											        			   $('#grid').datagrid('clearSelections');
											        			   $.messager.show({
														        		title:'提示',
														        		height:170,
														        		msg:'操作成功'
														        	});
													});
												}
											});
										}
									}
								});								
							} else {
								$.messager.alert('提示','请选择一条记录操作');
							}				
						}					
					},{
						text:'删除流向单',
						iconCls:'icon-remove',
						handler:function(){								
							var arr = $('#grid').datagrid('getSelections');
							if(arr.length < 1){
								$.messager.alert('提示','至少选择一条流向单删除','warning');
							} else {
								 $.messager.confirm('确认','确定删除所选记录吗？',function(r){   
									       if (r){   
									           var idStr = '';
									           for(var i=0;i < arr.length;i++ ){
									        		 idStr += arr[i].id + ',';
									        	 };
									           idStr = idStr.substring(0,idStr.length-1);
									           $.post('json/flowCard_delete.action',
									        		   {idStr:idStr},
									        		   function(){
									        			   $('#grid').datagrid('reload');
									        			   $('#grid').datagrid('clearSelections');
									        			   $.messager.show({
												        		title:'提示',
												        		height:170,
												        		msg:'删除成功'
												        	});
									        		   });
									       }   
									   }); 
							}
						}
					},'-',{
						text:'新增流向单明细' ,
						iconCls:'icon-tableAdd' ,
						handler:function(){
							var arr = $('#grid').datagrid('getSelections');
							if(arr.length == 1){
								$.ajax({
									url:'json/flowCardDetail_isExist.action',
									cache:false,
									data:{flowCardId:arr[0].id},
									dataType:'json',
									success:function(result){
										//异步检查明细是否存在
										if(result.status == "exist"){
											$.messager.alert('提示',result.msg);
										} 
										if(result.status == "notExist"){
											$('#dialog1').dialog({
												title:'新增流向单明细' 
											});
											$('#myform1').form('clear');
											$('#dialog1').dialog('open');
											document.getElementById("flowCardId").value=arr[0].id;
											//异步加载
											$('#itemName').combobox({
												 url:'json/item_getMaterial.action',
												 editable:false,
												 valueField:'id',   
												 textField:'name',
												 onSelect:function(record){
														document.getElementById("itemId").value=record.id;
														document.getElementById("itemId").readOnly=true;
												}
											 });
											//异步加载
											 $('#aclientName').combobox({
												 url:'json/aClient_getDemander.action',
												 editable:false,
												 valueField:'id',   
												 textField:'name',
												 onSelect:function(record){
														document.getElementById("aclientId").value=record.id;
														document.getElementById("aclientId").readOnly=true;
												}
											 });
										}
									}
								});
							} else {
								$.messager.alert('提示','请选择一条流向单修改');
							}
						}
					}]
			  });
			
			$('#save').click(function(){
				if(flag == 'add'){
				    $('#myform').form('submit', {   
					        url:'json/flowCard_add.action', 
					        onSubmit: function(){  
					        	if(!$('#myform').form('validate')){
					        		return false;
					        	}
					        },   
					        success:function(){  
					        	$('#dialog').dialog('close');
					        	$('#grid').datagrid('reload');
					        	$.messager.show({
					        		title:'提示',
					        		height:170,
					        		msg:'操作成功'
					        	});
					       }   				
				    });	
				} else {
					 $('#myform').form('submit', {   
					        url:'json/flowCard_modify.action', 
					        onSubmit: function(){  
					        	if(!$('#myform').form('validate')){
					        		return false;					        	
					        	}
					        },   
					        success:function(){  
					        	$('#dialog').dialog('close');
					        	$('#grid').datagrid('reload');
					        	$.messager.show({
					        		title:'提示',
					        		height:170,
					        		msg:'修改成功'
					        	});
					       }   				
				    });	
				}
		  	});
			
			$('#save1').click(function(){
			    $('#myform1').form('submit', {   
				        url:'json/flowCardDetail_add.action', 
				        onSubmit: function(){  
				        	if(!$('#myform1').form('validate')){
				        		return false;
				        	}
				        },   
				        success:function(){  
				        	$('#dialog1').dialog('close');
				        	$('#grid').datagrid('reload');
				        	$.messager.show({
				        		title:'提示',
				        		height:170,
				        		msg:'修改成功'
				        	});
				       }   
				   });		 
		  	});
			
			$('#cancel,#cancel1').click(function(){
		    	$('#dialog,#dialog1').dialog('close');
		    });
			
			$('#count').numberbox({
				min:0 , 
				required:true , 
				missingMessage:'数量必填!' ,
			});
		});
	</script>

  </head>
  
  <body>
  	<table id="grid"></table>
	<div id="dialog" class="easyui-dialog" closed=true resizable=false modal=true style="width:450px;">  	     	 
			<form id="myform" action="" method="post">
			<input id="id" type="hidden" name="flowCard.id" value="">		
			<input id="recorderId" type="hidden" name="flowCard.recorder.id" value="">				
		  		<table>		  			
		  			<tr>
		  				<td>供方分销商号</td>
		  				<td>
		  					<input id="clientId" name="flowCard.client.id" class="easyui-validatebox" required=true 
		  						missingMessage="供方分销商代码必填" style="width:200px;">					
		  				</td>	  				 				
		  			</tr>
		  			<tr>
		  				<td>供方分销商名称</td>
		  				<td>
		  					<input id="clientName" type="text" name="" style="width:200px;">	  					
		  				</td>
		  			</tr>
		  			<tr>
		  				<td>录入人</td>
		  				<td>
		  					<input id="recorderName" type="text" style="width:200px;" value="" readonly="readonly">	  					
		  				</td>
		  			</tr>											  					  	
		  			<tr>
		  				<td align="center" colspan="2">
		  					<a id="save" class="easyui-linkbutton">保存</a>
		  					<a id="cancel" class="easyui-linkbutton">取消</a>
		  				</td>
		  			</tr>
		  		</table>	
		  	</form>			  	
		  </div>  
		  
		  <div id="dialog1" class="easyui-dialog" closed=true resizable=false modal=true style="width:450px;">  	     	 
			<form id="myform1" action="" method="post">
				<input type="hidden" id="flowCardId" name="flowCardDetail.flowCard.id" value="">
		  		<table>		  					  			
		  			<tr>
		  				<td>物料代码</td>
		  				<td>
		  					<input id="itemId" name="flowCardDetail.item.id" class="easyui-validatebox" required=true 
		  						missingMessage="供方分销商代码必填" style="width:200px;">					
		  				</td>	  				 				
		  			</tr>
		  			<tr>
		  				<td>物料名称</td>
		  				<td>
		  					<input id="itemName" type="text" name="" style="width:200px;">	  					
		  				</td>
		  			</tr>
		  			<tr>
		  				<td>需方客户号</td>
		  				<td>
		  					<input id="aclientId" name="flowCardDetail.aclient.id" class="easyui-validatebox" required=true 
		  						missingMessage="供方分销商代码必填" style="width:200px;">					
		  				</td>	  				 				
		  			</tr>
		  			<tr>
		  				<td>需方客户名称</td>
		  				<td>
		  					<input id="aclientName" type="text" name="" style="width:200px;">	  					
		  				</td>
		  			</tr>	
		  			<tr>
		  				<td>需方数量</td>
		  				<td>
		  					<input id="count" type="text" name="flowCardDetail.count" style="width:200px;">	  					
		  				</td>
		  			</tr>  	
		  			<tr>
		  				<td align="center" colspan="2">
		  					<a id="save1" class="easyui-linkbutton">保存</a>
		  					<a id="cancel1" class="easyui-linkbutton">取消</a>
		  				</td>
		  			</tr>
		  		</table>	
		  	</form>			  	
		  </div> 	
  </body>
</html>
